﻿@using Microsoft.AspNetCore.Components.Forms
@using Shared.Extensions
<div style="min-height:80vh">
    <Collapse OnCollapseChanged="(aa)=>{   return Task.CompletedTask;}" IsAccordion="true">
        <CollapseItems>
            <CollapseItem Text="通用属性" IsCollapsed="false">

                <ValidateForm @ref="ValidateForm" Model="@ComponentData" OnValidSubmit="@OnValidComplexModel" OnFieldValueChanged="OnFieldValueChanged">
                    <EditorForm TModel="ContainerDto" ItemsPerRow="1" AutoGenerateAllItem="false">

                        <FieldItems>
                            <EditorItem @bind-Field="@context.Id"></EditorItem>
                            <EditorItem @bind-Field="@context.MutLanguage">
                                <EditTemplate Context="value">
                                    <Collapse>
                                        <CollapseItems>
                                            <CollapseItem Text="多语言设置">
                                                <BootstrapInput @bind-Value="value.MutLanguage.zh_CN" OnValueChanged="(v)=>{return FormDesigner.StateHasChangedAsync();}"></BootstrapInput>
                                                <BootstrapInput @bind-Value="value.MutLanguage.zh_TW" OnValueChanged="(v)=>{return FormDesigner.StateHasChangedAsync();}"></BootstrapInput>
                                                <BootstrapInput @bind-Value="value.MutLanguage.en_US" OnValueChanged="(v)=>{return FormDesigner.StateHasChangedAsync();}"></BootstrapInput>
                                                <BootstrapInput @bind-Value="value.MutLanguage.ja_JP" OnValueChanged="(v)=>{return FormDesigner.StateHasChangedAsync();}"></BootstrapInput>
                                            </CollapseItem>
                                        </CollapseItems>
                                    </Collapse>
                                </EditTemplate>
                            </EditorItem>
                           
                        </FieldItems>
                    </EditorForm>
                </ValidateForm>

            </CollapseItem>

            <CollapseItem Text="Ext(扩展属性)">
                @if (ComponentData is not null)
                {
                    @foreach (var item in @ComponentData.Props)
                    {
                        @switch (item.Value.PType)
                        {
                            case PType.String:
                                <BootstrapInput OnValueChanged="(e)=>{OnPropChanged.InvokeAsync(new PInof(){key=item.Key,Value=e});return Task.CompletedTask;}" ShowLabel="true" DisplayText="@item.Key" @bind-Value="@item.Value.StringValue"></BootstrapInput>
                                break;
                            case PType.SingleColor:
                                <Select ShowLabel="true" DisplayText="@item.Key" Color="Color.Primary" IsClearable="true" Items="ColorItems" @bind-Value="@item.Value.StringValue"></Select>
                                break;
                            case PType.Bool:
                                <Checkbox TValue="bool" DisplayText="@item.Key"
                                          ShowLabel="true" class="form-control"
                                          Color="Color.Danger"
                                @bind-Value="@(item.Value.BoolVal)"></Checkbox>
                                break;
                            case PType.Int:
                                break;
                            case PType.Double:
                                break;
                            case PType.DataSourse:
                                <Select IsVirtualize="true" @bind-Value="@item.Value.StringValue" OnQueryAsync="OnQueryAsync" ShowLabel="true" DisplayText="@item.Key" Color="Color.Primary" IsClearable="true"></Select>
                                break;
                            default:
                                <BootstrapInput ShowLabel="true" DisplayText="@item.Key" @bind-Value="@item.Value.StringValue"></BootstrapInput>
                                break;
                        }
                    }
                }
            </CollapseItem>

        </CollapseItems>
    </Collapse>
</div>




@code {
    [CascadingParameter(Name = "Root")]
    public FormDesigner FormDesigner { get; set; }

    [Parameter]
    public ContainerDto? ComponentData { get; set; }

    [NotNull]
    private ValidateForm? ValidateForm { get; set; }

    [Parameter]
    public EventCallback<ChangeEventArgs> OnLabelChanged { get; set; }
    [Parameter]
    public EventCallback<ChangeEventArgs> OnWidthChanged { get; set; }
    [Parameter]
    public EventCallback<ChangeEventArgs> OnHeightChanged { get; set; }
    [Parameter]
    public EventCallback<PInof> OnPropChanged { get; set; }
    /// <summary>
    /// Primary / Secondary / Success / Danger / Warning / Info / Dark
    /// </summary>
    List<SelectedItem> ColorItems = new List<SelectedItem>() {
                new SelectedItem("None", "None"),
                new SelectedItem("Active", "Active"),
                new SelectedItem("Primary", "Primary"),
                new SelectedItem("Warning", "Warning"),
                new SelectedItem("Info", "Info"),
                new SelectedItem("Danger", "Danger"),
                new SelectedItem("Success", "Success"),
                new SelectedItem("Dark", "Dark")
                };
    List<SelectedItem> BoolItems = new List<SelectedItem>() {
                new SelectedItem("True", "True"),
                new SelectedItem("False", "False"),

                };
    private SelectedItem? VirtualItem1 { get; set; }
    private async Task<QueryData<SelectedItem>> OnQueryAsync(VirtualizeQueryOption option)
    {
        await Task.Delay(200);
        return new QueryData<SelectedItem>
            {
                Items = new List<SelectedItem>()
                        {
                    new SelectedItem("北京", "北京"),
                    new SelectedItem("上海", "上海") { Active = true },
                },
                TotalCount = 2
            };
    }
    public void OnValueChanged(MouseEventArgs e)
    {

        //Color.Dark
    }
    public void OnFieldValueChanged(string key, object value)
    {//lstNames.GroupBy(n => n).Any(c => c.Count() > 1);
        if (key == "Id")
        {

            if (FormDesigner.FunctionPage.ContainerData.FindAllComponent(x => true).GroupBy(x => x.Id).Any(x => x.Count() > 0))
            {
                ValidateForm.SetError("Id", "数据库中已存在");
            }

        }
        //return Task.CompletedTask;
    }
    private Task OnValidComplexModel(EditContext context)
    {
        ValidateForm.SetError("Id", "数据库中已存在");
        return Task.CompletedTask;
    }
    /// <summary>
    /// 当设置控件的Id属性的时候
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public Task IdOnValueChanged(string v)
    {

        //if (FormDesigner.ContainerData.FindAll(x => true).GroupBy(x => x.Id).Any(x => x.Count() > 1))
        //{
        //    ComponentData.Id = "";
        //}
        return Task.CompletedTask;
    }
    /// <summary>
    /// 当设置控件的Id属性的时候
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public async Task OnValueChangedForBool(bool v)
    {
        await FormDesigner.StateHasChangedAsync();
    }
    /// <summary>
    /// 当设置控件外观颜色的时候
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public async Task OnValueChangedForColor(Color v)
    {
        await FormDesigner.StateHasChangedAsync();
    }
    /// <summary>
    /// 当设置控件外观颜色的时候
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public async Task OnValueChangedForInt(int v)
    {
        await FormDesigner.StateHasChangedAsync();
    }
    /// <summary>
    /// 当设置控件外观颜色的时候
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public async Task OnValueChangedForString(string v)
    {
        await FormDesigner.StateHasChangedAsync();
    }
}
